
;
var ServerTasks;
if (!ServerTasks) ServerTasks = {};
(function() {
    var st = ServerTasks;
    st.tasks = [];
    st.task_stats = {};
    st.task_ranks = {};
    st.task_count = 0;

    st.init = function(taskname) {
        $("#tasks")
            .append($("<p>")
                    .attr("id", taskname)
                    .attr("class", "bounceInRight")
		    .html("<img id='" + taskname + "_img' width='20px' height='20px' src='../img/arrow.png'>" + "<span>" + taskname + "</span>"));
        st.task_stats[taskname] = "stop";
    };
    st.task_start = function(taskname, count) {
        $("#" + taskname)
            .attr("class", "bounceInRight")
            .css({
                top: (40 * count) + "px",
                display: "block"
            });
	$("#tasks")
	    .append($("<p>")
		.attr("id", taskname)
		.attr("class", "boundInRight")
		.html(taskname));
        st.task_ranks[taskname] = count;
    };

    st.task_end = function(taskname) {
       	if (taskname != "stopDBMS") {
	$("#"+taskname).html("<img id = '" + taskname + "_img' width='20px' height='20px' src='../img/circle_2.png'>" + "<span>" + taskname + "</span>" + "</img>");
	} else {
		$("#"+taskname).html("<img id = '" + taskname + "_img' width='20px' height='20px' src='../img/cross.png'>" + "<span>" + taskname + "</span>" + "</img>");
	}
    };

    /*
    st.task_end = function(taskname) {
        var task_rank = st.task_ranks[taskname];
        $("#" + taskname).attr("class", "fadeOutUp");
        setTimeout(function() {
            for (var tp in st.task_ranks) {
                var tr = st.task_ranks[tp];
                if (tr > task_rank) {
                    st.task_ranks[tp] = tr - 1;
                    $("#" + tp).animate({ top: "-=40px"}, "slow");
                }
            }
        }, 1000);
        delete st.task_ranks[taskname];
    };
    */

    function spinerFunc(func, args) {
        $("#spiner").css("display", "block");
        setTimeout(function() {
            $("#spiner").css("display", "none");
            func.apply(func, args);
        }, 300);
    };

    st.stop_all = function() {
        spinerFunc(function() {
            $("#tasks p").attr("class", "fadeOutUp");
        });
        st.task_ranks = {};
        st.task_stats = {};
        st.task_count = 0;
        $("#pageTitle")
            .colorBlend("stop", "all")
            .css("color", "white");
        $("#server").attr("class", "");
    };

    st.alert = function(taskname) {
	var str = taskname + "_img";
	console.log(str);
	$("#" + str).attr("src", "../img/cross.png");
	$("#pageTitle").colorBlend("color", ["white", "red"], "duration", 1000);
	$("#alert").css({
		display: "block",
		    });
	$("#alert").attr("class", "flash");
    }

    st.proc = function(task, curr_stat) {
        if (st.tasks.indexOf(task) == -1) {
            st.tasks.push(task);
            st.init(task);
        }
        var task_stat = st.task_stats[task];
        if (curr_stat != task_stat) {
            var curr_count = st.task_count;
	    //            st.task_count += (curr_stat == "task_start") ? 1 : -1;
            st.task_count += (curr_stat == "task_start") ? 1 : 0;
	    if (curr_stat == "task_end") {
		st.task_end(task);
	    }
            spinerFunc(st[curr_stat], [task, curr_count]);
            st.task_stats[task] = curr_stat;
            if (st.task_count > 0) {
                $("#pageTitle").colorBlend("color", ["white", "lime"], "duration", 1000);
                //$("#server").attr("class", "bounce");
            } else {
                $("#pageTitle")
                    .colorBlend("stop", "all")
                    .css("color", "white");
                //$("#server").attr("class", "");
            }
        }
    };

    var serveraddr = {
        diag : "192.168.0.210",
        db1  : "192.168.0.202",
        web1 : "192.168.0.204",
        web2 : "192.168.0.203",
        local : "localhost"
    };
    
    st.mainloop = function(server) {
	var stateLock = 0;
        var socket = io.connect("http://" + serveraddr[server] + ":2525/");
        socket.on("connect", function () {
            console.log("connected");
            $("#pageTitle").css("color", "white");
        });
        socket.on("message", function (msg) {
            msg = $.trim(msg);

            if (msg == "stop_all_task") {
                st.stop_all();
		stateLock = 0;
            } else if (msg.match(/failed/) != null) {
		var data = msg.split(":");
		st.alert(data[0]);

	    } else {
                var data = msg.split(":");

                if (data.length == 2) {
                    st.proc(data[1], data[0]);
                }

            }
        });
        socket.on("disconnect", function(){
            console.log("disconnected");
            st.stop_all();
        });
        setInterval(function() {
            if (!socket.socket.connected) {
                $("#pageTitle").css("color", "lightgray");
                console.log("connecting...");
                socket.socket.connect();
            }
        }, 1000);
    };
})();

$(document).ready(function() {
    var servername = location.href.match(/\/([^\/]*)\/[^\/]*\.html/)[1];
    var title = "ServerTasks:" + servername;
    document.title = title;
    $("#main").attr("title", title);
    ServerTasks.mainloop(servername);
});
